package com.longrise.android.byjk.plugins.publicWeb;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.OpenLoginActivity;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.PersonalExhibitionActivity;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.PersonalInfoActivity;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.PersonalPresenter;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.change.BundlePhoneActivity;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.change.ChangeInfoActivity;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.product_show.AddProductShowActivity;
import com.longrise.android.byjk.plugins.tabfirst.CustomerModuleJumpHelper;
import com.longrise.android.byjk.plugins.productfeerate.UploadProductfeerateActivity;
import com.longrise.android.byjk.plugins.vip.dailysign.DailySignActivity;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.byjk.utils.Tracker_Google;
import com.longrise.android.by.activity.BYWebActivity;
import com.longrise.android.by.utils.BYContants;

/**
 * Created by callxkj on 2017/3/28.
 * 获取相应web的url地址,并跳转到web页面
 * <p>
 * 也可以从web页面跳转到其他页面
 */

public class PublicWebJumpUtil {
    private static final String TAG = "PublicWebJumpUtil";

    public static void jumpToPubWeb(final BaseActivity activity, final String urltype) {
        EntityBean bean = new EntityBean();
        bean.set("devicetype", "android");
        bean.set("urltype", urltype);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        activity.showLoadingDialog();
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_getAppEntryPath", fbean, new LoadDataManager.OnRequestCallListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    if (result != null) {
                        EntityBean resultBean = (EntityBean) result;
                        parseResult(activity, urltype, resultBean);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                activity.showToast(AppUtil.getString(R.string.nonetwork));
            }

            @Override
            public void onFinished(String key, String service) {
                activity.dismissLoadingDialog();
            }
        });
    }

    /**
     * 解析结果并跳转
     */
    private static void parseResult(BaseActivity activity, String urltype, EntityBean bean) {
        EntityBean resultBean = bean.getBean("result");
        int resultstate = (Integer) bean.get("resultstate");
        String resultdesc = bean.getString("resultdesc");
        if (resultstate == 1) {
            String pageurl = resultBean.getString("pageurl");
            Bundle bundle = new Bundle();
            int screenWidth = AppUtil.getScreenWidth();
            int widthDP = AppUtil.px2dip(screenWidth);
            String userid = UserInfor.getInstance().getUserid();
            switch (urltype) {
                case "jfrw": //积分任务 赚取积分
                    String usersfzh = UserInfor.getInstance().getUsersfzh();
                    pageurl = pageurl + "?cardno=" + usersfzh + "&userid=" + userid + "&screenwidth=" + widthDP;
                    break;
                case "qdgz": //签到规则 添加屏幕宽度
                case "jfgz": //积分规则
                    pageurl = pageurl + "?screenwidth=" + widthDP + "&userid=" + userid;
                    break;
            }
            bundle.putString(PublicWebActivity.URL, pageurl);
            activity.startActivity(PublicWebActivity.class, bundle);

        } else {
            activity.showToast(resultdesc);
        }
    }

    /**
     * 从这个公共web页面跳转到其他页面
     */
    public static void jumpToActivity(Activity activity, String title, Bundle bundle) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        switch (title) {
            case PublicWebPresenter.MRQD: //每日签到
                jump(activity, DailySignActivity.class, bundle);
                break;
            case PublicWebPresenter.ZYRZ: //执业认证 app现在不能更改不做处理了
                break;
            case PublicWebPresenter.SFRZ: //身份认证 需要姓名和身份证都完善才可以
                jump(activity, PersonalInfoActivity.class, bundle);
                break;
            case PublicWebPresenter.NC: //昵称
                bundle.putInt("type", PersonalPresenter.CHANGENICKNAME);
                jump(activity, ChangeInfoActivity.class, bundle);
                break;
            case PublicWebPresenter.GRJJ: //个人简介
                bundle.putInt("type", PersonalPresenter.CHANGEPROFILE);
                jump(activity, ChangeInfoActivity.class, bundle);
                break;
            case PublicWebPresenter.CPFA: //产品方案
                jump(activity, AddProductShowActivity.class, bundle);
                break;
            case PublicWebPresenter.KTKJDL: //开通快捷登录
                jump(activity, OpenLoginActivity.class, bundle);
                break;
            case PublicWebPresenter.ZYXFX: //展业秀分享
                jump(activity, PersonalExhibitionActivity.class, bundle);
                break;
            case PublicWebPresenter.SCFL: //上传费率
                tuUploadfee(activity, bundle);
                addEvent("费率活动介绍", "立即上传", "");
                break;
            case PublicWebPresenter.BYZP: //保易智评
//                jump(activity, XQPCActivity.class, bundle);  //需要vip判断
                CustomerModuleJumpHelper.toJump(activity, "1006", "1006", "", "", "", "");
            case PublicWebPresenter.KHGL: //客户管理
//                jump(activity, ClientManagerActivity.class, bundle);  //需要vip判断
                CustomerModuleJumpHelper.toJump(activity, "1007", "1007", "", "", "", "");
                break;
            case PublicWebPresenter.CPDP: //产品点评
                bundle.putInt(BYContants.BY_EXTRA_PARAMS, BYContants.PRODUCT_TPYE);
                jump(activity, BYWebActivity.class, bundle);
                break;
            case PublicWebPresenter.GSDP: //公司点评
                bundle.putInt(BYContants.BY_EXTRA_PARAMS, BYContants.COMPANY_TYPE);
                jump(activity, BYWebActivity.class, bundle);
                break;
            case PublicWebPresenter.BXAL: //保险案例
                bundle.putInt(BYContants.BY_EXTRA_PARAMS, BYContants.TRYELIKE_TYPE);
                jump(activity, BYWebActivity.class, bundle);
                break;
            case PublicWebPresenter.JHDB: //计划对比
                bundle.putInt(BYContants.BY_EXTRA_PARAMS, BYContants.PLANCONSTART_TYPE);
                jump(activity, BYWebActivity.class, bundle);
                break;
        }
    }

    private static void jump(Context context, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
//        bundle.putString(BBWebHelper.FROM_PAGE, BBWebHelper.PAGE_NAME);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    /**
     * 上传产品费率
     */
    private static void tuUploadfee(Activity activity, Bundle bundle) {
        String userphone = UserInfor.getInstance().getUserphone();
        if (TextUtils.isEmpty(userphone)) {  //手机号码为空
            showBundleNumberTips(activity);
            return;
        }
        jump(activity, UploadProductfeerateActivity.class, bundle);
    }


    /**
     * 显示提示手机号码为空的dialog
     */
    private static void showBundleNumberTips(final Activity activity) {
        View view = View.inflate(activity, R.layout.dialog_uploadfee_bundlnumber, null);
        DialogUtil.getInstance().creatAlertDialog(activity, view, 266, 171);
        TextView tv_cancel = (TextView) view.findViewById(R.id.text_cancle);
        TextView tv_confirm = (TextView) view.findViewById(R.id.text_confirm);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, BundlePhoneActivity.class);
                activity.startActivity(intent);
                DialogUtil.getInstance().dismiss();
            }
        });
    }

    private static void addEvent(String cate, String action, String lable) {
        Tracker_Google.getInstance().addEvent(cate, action, lable);
    }
}
